﻿using System;
using System.Collections.Generic;
using System.Text;

using Gx.Dal.Manage;//数据
using Gx.Model.Entity;//实体
using Gx.Common;
using Gx.Model.Role;
using Gx.Bll.Role;
using Gx.Model;
using Gx.Model.Enums;
using Gx.Model.ViewModel;
using System.Web.ModelBinding;
using System.Linq;

namespace Gx.Bll.Manage
{
    public class B_EmployeeInfoBll
    {
        /// <summary>
        /// 获取员工数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<B_EmployeeInfo> GetB_EmployeeInfoList(EmployeeInfoInput model,
        int pageIndex, int pageSize, out int totalCount)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                int PageIndex = (pageIndex - 1) * pageSize + 1;
                int PageSize = pageIndex * pageSize;
                List<B_EmployeeInfo> list = dal.GetB_EmployeeInfoList(model, PageIndex, PageSize, out totalCount);
                if (list.Count == 0)
                {
                    return list;
                }
                return list;
            }
        }

        /// <summary>
        /// 添加员工的基本信息
        /// </summary>
        /// <param name="model">基本员工实体</param>
        /// <param name="jobModel">岗位信息实体</param>
        /// <returns>成功返回true,失败则是false</returns>
        public ReturnModel AddEmployeeInfo(B_EmployeeInfo model)
        {
            ReturnModel r = new ReturnModel()
            {
                Msg = "操作失败"
            };
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                int isExistsMobile = IsExistsMobile(model.EmployeeId, model.EmpMobile);
                if (isExistsMobile > 0)
                {
                    r.Msg = "请勿添加重复的手机号码";
                    return r;
                }
                LKCompany company = new LKCompanyBll().GetLKCompany(model.CompanyId);
                E_DepartmentInfo dep = new E_DepartmentInfoBll().GetDepModel(model.DepId);
                E_LevelInfo lvl = new E_LevelInfoBll().GetLevelInfoModel(model.LevelId);
                M_RoleInfo role = new M_RoleInfoBll().GetRoleInfoModel(model.RoleId);
                model.DepName = dep.DepName;
                model.LevelName = lvl.LevelName;
                model.RoleName = role.RoleName;
                model.AddTime = model.LastEditTime = DateTime.Now;
                model.CompanyName = company.CompanyName;
                model.AddUser = model.LastEditUser = LoginHelper.GetUserId();
                model.EmpPwd = Base64Helper.Encryption("123456", ConfigHelper.CookieSecretKey);
                int res = dal.AddB_EmployeeInfo(model);
                if (res == 0)
                {
                    return r;
                }
                r.Msg = "操作成功!";
                r.Success = true;
                return r;
            }
        }


        /// <summary>
        /// 编辑员工基本信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="jobModel"></param>
        /// <returns></returns>
        public ReturnModel EditEmployeeInfo(B_EmployeeInfo model)
        {
            ReturnModel r = new ReturnModel()
            {
                Msg = "操作失败",
            };
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                int isExistsMobile = IsExistsMobile(model.EmployeeId, model.EmpMobile);
                if (isExistsMobile > 0)
                {
                    r.Msg = "请勿添加重复的手机号码";
                    return r;
                }
                LKCompany company = new LKCompanyBll().GetLKCompany(model.CompanyId);
                E_DepartmentInfo dep = new E_DepartmentInfoBll().GetDepModel(model.DepId);
                E_LevelInfo lvl = new E_LevelInfoBll().GetLevelInfoModel(model.LevelId);
                M_RoleInfo role = new M_RoleInfoBll().GetRoleInfoModel(model.RoleId);
                model.DepName = dep.DepName;
                model.LevelName = lvl.LevelName;
                model.RoleName = role.RoleName;
                model.LastEditTime = DateTime.Now;
                model.LastEditUser = LoginHelper.GetUserId();
                model.CompanyName = company.CompanyName;
                int res = dal.EditB_EmployeeInfo(model);
                if (res == 0)
                {
                    return r;
                }
                r.Msg = "操作成功!";
                r.Success = true;
                return r;
            }
        }

        /// <summary>
        /// 删除员工基本信息
        /// </summary>
        /// <param name="EmployeeId"></param>
        /// <returns></returns>
        public ReturnModel DelEmployeeInfo(int EmployeeId)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Msg = "操作失败",
                };
                bool res = dal.DelB_EmployeeInfo(EmployeeId, LoginHelper.GetUserId());
                if (res)
                {
                    r.Msg = "操作成功";
                    r.Success = true;
                }
                return r;
            }
        }
        public B_EmployeeInfo GetB_EmployeeInfoModel(int EmployeeId)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                return dal.GetB_EmployeeInfoModel(EmployeeId);
            }
        }

        public List<B_EmployeeInfo> GetB_EmployeeInfoList(List<int> empId)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                return dal.GetB_EmployeeInfoList(empId);
            }
        }


        public ReturnModel EditPassWord(RestPassWordInput input)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Msg = "操作失败",
                };
                if (input.EmpPwd.IsNullOrEmpty())
                {
                    r.Msg = "密码不能为空";
                    return r;
                }
                input.EmployeeId = LoginHelper.GetUserId();
                input.EmpPwd = Base64Helper.Encryption(input.EmpPwd, ConfigHelper.CookieSecretKey);
                int res = dal.RestPassWord(input);
                if (res == 0)
                {
                    return r;
                }
                r.Msg = "操作成功!";
                r.Success = true;
                return r;
            }
        }

        public ReturnModel RestPassWord(RestPassWordInput input)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Msg = "操作失败",
                };
                input.EmpPwd = Base64Helper.Encryption("123456", ConfigHelper.CookieSecretKey);
                int res = dal.RestPassWord(input);
                if (res == 0)
                {
                    return r;
                }
                r.Msg = "操作成功!";
                r.Success = true;
                return r;
            }
        }


        /// <summary>
        /// 判断是否存在手机号码
        /// </summary>
        /// <param name="EmployeeId"></param>
        /// <param name="EmpMobile"></param>
        /// <returns></returns>
        public int IsExistsMobile(int EmployeeId, string EmpMobile)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                return dal.IsExistsMobile(EmployeeId, EmpMobile);
            }
        }

        /// <summary>
        /// 根据登录账号和密码判断是否存在该账号
        /// </summary>
        /// <param name="userLogin">登录账号</param>
        /// <param name="userPwd">登录密码</param>
        /// <param name="result">登陆状态</param>
        /// <returns></returns>
        public ReturnModel<string> UserLogin(string userLogin, string userPwd)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                ReturnModel<string> r = new ReturnModel<string>();
                string md5Pwd = Base64Helper.Encryption(userPwd, ConfigHelper.CookieSecretKey);
                B_EmployeeInfo m = dal.Login(userLogin, md5Pwd);
                if (m == null)
                {
                    r.Msg = "账户或密码不存在!";
                    return r;
                }
                CookieHelper.RemoveCookie(LoginHelper.UserCookie);
                List<string> key = new List<string>
                {
                    LoginHelper.GetCookieEncryption(GlobalHelper.COOKIEUSERID),
                    LoginHelper.GetCookieEncryption(GlobalHelper.COOKIEUSERNAME),
                    LoginHelper.GetCookieEncryption(GlobalHelper.COOKIEROLEID),
                };
                List<object> value = new List<object>
                {
                    LoginHelper.GetCookieEncryption(m.EmployeeId.ToString()),
                    LoginHelper.GetCookieEncryption(m.EmpName),
                    LoginHelper.GetCookieEncryption(m.RoleId.ToString()),
                };
                CookieHelper.SetCookieValues(LoginHelper.UserCookie, key.ToArray(), value.ToArray(), DateTime.Now.AddHours(8));
                r.Success = true;
                r.Msg = "ok!";
                r.Data = "/Home/Index";
                return r;
            }
        }



        /// <summary>
        /// 获取员工与仓库的关系
        /// </summary>
        /// <param name="EmployeeId"></param>
        /// <returns></returns>
        public List<int> GetUserWareHouseList(int EmployeeId)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                return dal.GetUserWareHouseList(EmployeeId);
            }
        }



        /// <summary>
        /// 添加仓库与人员关系
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnModel AddUserWareHouse(UserWareHouseInput input)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                ReturnModel r = new ReturnModel() { Msg = "添加失败" };
                if (input.EmployeeId > 0 && input.WareHouseId == null)
                {
                    dal.DelLK_UserWareHouse(input.EmployeeId);
                    r.Msg = "删除成功";
                    r.Success = true;
                    return r;
                }
                if (input.EmployeeId == 0 || input.WareHouseId == null || input.WareHouseId.Count == 0)
                {
                    r.Msg = "参数错误";
                    return r;
                }
                dal.DelLK_UserWareHouse(input.EmployeeId);
                foreach (var item in input.WareHouseId)
                {
                    dal.AddLK_UserWareHouse(new LKUserWareHouse() { UserId = input.EmployeeId, WareHouseId = item });
                }
                r.Msg = "操作成功";
                r.Success = true;
                return r;
            }
        }


        /// <summary>
        /// 获取员工被分配的冷库
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public List<UserWareHouseVM> GetList(List<int> UserId)
        {
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                return dal.GetList(UserId);
            }
        }



        /// <summary>
        /// 获取员工前10条数据
        /// </summary>
        /// <param name="empName"></param>
        /// <returns></returns>
        public List<B_EmployeeInfo> GetE_EmployeeInfoListLikeFront10(string empName)
        {
            if (empName == null)
            {
                empName = "";
            }
            using (B_EmployeeInfoDal dal = new B_EmployeeInfoDal())
            {
                List<B_EmployeeInfo> empBasicList = dal.GetE_EmployeeInfoListLikeFront10(empName);
                return empBasicList;
            }
        }
    }
}
